package com.tdam2014.grupo3.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.graphics.Bitmap;
import android.os.Environment;

public class ImagesUtils {
	
	private static final String ROOT_PATH = Environment.getExternalStorageDirectory().toString() + File.separator + "Flickr Viewer" + File.separator;
	public static final String PROFILES_PATH = ROOT_PATH + "Profiles" + File.separator;
	public static final String THUMBS_PATH = ROOT_PATH + "Thumbs" + File.separator;
	
	public static boolean saveImage(Bitmap image, String path) {
		File fileImage = new File(path);
		
		try {
			FileOutputStream fileOutputStream = new FileOutputStream(fileImage);
			image.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
			fileOutputStream.flush();
		} catch (FileNotFoundException ex) {
			ex.printStackTrace();
			return false;
		} catch (IOException ex) {
			ex.printStackTrace();
			return false;
		}
		
		return true;
	}
	
	public static void deleteImage(String path) {
		File fileImage = new File(path);
		if(fileImage.exists()) fileImage.delete();
	}
	
	public static void verifyDirectoriesExistence() {
		File profilesDirectory = new File(PROFILES_PATH);
		if(!profilesDirectory.exists()) profilesDirectory.mkdirs();

		File thumbsDirectory = new File(THUMBS_PATH);
		if(!thumbsDirectory.exists()) thumbsDirectory.mkdirs();
	}
}
